package com.xianzhiOA.bean;

import com.xianzhiOA.util.MyReflectUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * Created by Administrator on 2017/5/19.
 */
public class SearchBean<T> {
    private T entity;
    private int page;
    private int rows;
    private Date beginDate;
    private Date endDate;

    public SearchBean(T entity,int page,int rows){
        this.entity = entity;
        if(page==0){
            this.page = 1;
        }else{
            this.page = page;
        }
        this.rows = rows;
    }

    public void makeSearch(T t) {
        try {
            Class entityClass = t.getClass();
            Field [] fs = entityClass.getDeclaredFields();
            //反射所有成员变量
            for (Field f : fs) {
                //get值
                String fieldGetName = MyReflectUtil.parGetName(f.getName());
                Method getM = entityClass.getMethod(fieldGetName,new Class[]{});
                Object fieldVal = getM.invoke(t, new Object[] {});
                //成员变量类型判断
                String fieldType = f.getType().getSimpleName();
                if(fieldVal!=null) {
                    //System.out.println(fieldVal);
                    String fieldSetName = MyReflectUtil.parSetName(f.getName());
                    Method setM = entityClass.getMethod(fieldSetName, f.getType());
                    //set值
                    if ("Integer".equals(fieldType)
                            || "int".equals(fieldType)) {
                        Integer intval = Integer.parseInt(fieldVal.toString());
                        if(intval!=0)setM.invoke(this.entity, intval);
                    } else if ("Long".equalsIgnoreCase(fieldType)) {
                        Long temp = Long.parseLong(fieldVal.toString());
                        if(temp!=0)setM.invoke(this.entity, temp);
                    } else if ("Double".equalsIgnoreCase(fieldType)) {
                        Double temp = Double.parseDouble(fieldVal.toString());
                        if(temp!=0)setM.invoke(this.entity, temp);
                    } else if ("Boolean".equalsIgnoreCase(fieldType)) {
                        Boolean temp = Boolean.parseBoolean(fieldVal.toString());
                        setM.invoke(this.entity, temp);
                    }else{
                        setM.invoke(this.entity, fieldVal);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        if(page!=0){
            this.page = page;
        }
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        if(rows!=0){
            this.rows = rows;
        }
    }

    public Date getBeginDate() {
        return beginDate;
    }

    public void setBeginDate(Date beginDate) {
        this.beginDate = beginDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
    public T getEntity() {
        return entity;
    }

    public void setEntity(T entity) {
        this.entity = entity;
    }

    @Override
    public String toString() {
        return "SearchBean{" +
                "entity=" + entity +
                ", page=" + page +
                ", rows=" + rows +
                ", beginDate=" + beginDate +
                ", endDate=" + endDate +
                '}';
    }
}
